package com.xjc.infra.util.security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.xjc.infra.util.PageFrom;

public class AESSecurity implements ISecurity {
    public static String KEY = "J2F45D@Z90~23U5-7O9P1&3L56*89Y)A";
    
    @Override
    public String getName() {
        return "AES";
    }

    @Override
    public byte[] desEncrypt(byte[] data) {
    	return desEncrypt(data,AESSecurity.KEY);
    
    }
    
    

    @Override
	public byte[] desEncrypt(byte[] data, String key) {
    	  if(PageFrom.isNullOrEmpty(key)){
    		  return null;
    	  }
    	  try {
              java.security.Security
                      .addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
              Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
              SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
              cipher.init(Cipher.DECRYPT_MODE, keyspec);
              byte[] original = cipher.doFinal(data);
              return original;
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          }
	}
    

	@Override
    public byte[] encrypt(byte[] data) {
		return encrypt(data,AESSecurity.KEY);
    }

	@Override
	public byte[] encrypt(byte[] data, String key) {
		 if(PageFrom.isNullOrEmpty(key)){
   		  	return null;
   	     }
		try {
            java.security.Security
                    .addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            cipher.init(Cipher.ENCRYPT_MODE, keyspec);
            byte[] encrypted = cipher.doFinal(data);
            return encrypted;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
	}
	
	

}
